package com.qs.home.ui.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.ocr.demo.FileUtil;
import com.google.gson.Gson;
import com.qs.base.adapter.ViewPagerAdaper;
import com.qs.base.base.BaseMyActivity;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.router.RouterFragmentPath;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeIdCardIdentifyResultsBinding;
import com.qs.home.entity.IdCardFrontResultEntity;
import com.qs.home.entity.TextResultEntity;
import com.qs.home.ui.VM.IdentifyDetailsViewModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * 身份证识别结果
 *
 * @Author ltzz
 * @Date 2019/8/22
 */
@Route(path = RouterActivityPath.Home.ID_CARD_IDENTIFY_RESULTS_ACTIVITY)
public class IDCardIdentifyResultsActivity extends BaseMyActivity<HomeIdCardIdentifyResultsBinding, IdentifyDetailsViewModel> implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private FragmentManager mFragmentManager;

    private ViewPagerAdaper viewPagerAdaper;
    List<Fragment> mFragmentList = new ArrayList<Fragment>();

    //识别结果数据
    @Autowired()
    String textResult;

    @Override
    protected boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected View titleLayout() {
        return binding.head;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.home_id_card_identify_results;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!StringUtils.isTrimEmpty(textResult)) {
            textResult=textResult.replace("IDCardResult front","");
            Gson gson = new Gson();
            try {
                IdCardFrontResultEntity idCardFrontResult = gson.fromJson(textResult, IdCardFrontResultEntity.class);
                viewModel.idCardFrontResult.set(idCardFrontResult);
                TextResultEntity textResultEn = new TextResultEntity();
                textResultEn.setFilePath(FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath());
                viewModel.textResultOb.set(textResultEn);
            } catch (Exception e) {
            }
        }

        mFragmentManager = getSupportFragmentManager();
        mFragmentList.add((Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.ORIGINAL_FRAGMENT).withParcelable("textResult", (Parcelable) viewModel.textResultOb.get()).navigation());
        mFragmentList.add((Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.ID_CARD_IDENTIFY_RESULTS_FRAGMENT).withParcelable("idCardFrontResult", (Parcelable) viewModel.idCardFrontResult.get()).navigation());

        viewPagerAdaper = new ViewPagerAdaper(mFragmentManager, mFragmentList);

        binding.original.setOnClickListener(this);
        binding.translate.setOnClickListener(this);

        initViewPager();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                setChangText(true, false);
                break;
            case 1:
                setChangText(false, true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void initViewPager() {
        binding.viewPg.addOnPageChangeListener(this);
        binding.viewPg.setAdapter(viewPagerAdaper);
        binding.viewPg.setCurrentItem(0);
        binding.viewPg.setOffscreenPageLimit(1);
        setChangText(true, false);
    }

    private void setChangText(boolean o, boolean thre) {
        binding.original.setSelected(o);
        binding.originalLine.setVisibility(o ? View.VISIBLE : View.GONE);
        binding.translate.setSelected(thre);
        binding.translateLine.setVisibility(thre ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onClick(View view) {

        int id = view.getId();
        if (id == R.id.original) {
            //原图
            setChangText(true, false);
            binding.viewPg.setCurrentItem(0);
        }
        if (id == R.id.character) {
            //信息
            setChangText(false, true);
            binding.viewPg.setCurrentItem(1);
        }
    }
}
